#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
    //先定义文件路径
        NSString * path = @"/Users/liuzhenlin/Downloads/00002.vcf";
    //定义错误值
        NSError * err;
    //给vcf分配空间，通过vcf路径进行解析
        NSString * vcf = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
    //判断错误是否为空，如果不为空就输出错误
        if(err!=nil){
            NSLog(@"%@",err);
        }
        
    //把vcf放进一个数组中,使用回车分割
        NSArray * arr = [vcf componentsSeparatedByString:@"\n"];
        NSMutableArray * res = [NSMutableArray new];
        
        NSMutableDictionary * dict;
        
        for(NSString * line in arr){
            NSLog(@"%@",line);
            
            if([line hasPrefix:@"BEGIN"]){
                NSLog(@"开始一条新纪录");
                dict = [NSMutableDictionary new];
                
            }
            if([line hasPrefix:@"END"]){
                NSLog(@"记录已结束");
                [res addObject:dict];
            }
            if([line hasPrefix:@"TEL"]){
                NSLog(@"找到了一条记录");
                NSArray * tels = [line componentsSeparatedByString:@"tel"];
                NSString * last = tels.lastObject;
                last = [last substringFromIndex:5];
                NSLog(@"%@",last);
                dict[@"tel"] = last;
            }
            if([line hasPrefix:@"FN"]){
                NSLog(@"找到了一个名字:%@",line);
                NSArray * names = [line componentsSeparatedByString:@":"];
                NSString * name = names.lastObject;
                name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                name = name.stringByRemovingPercentEncoding;
                NSLog(@"%@",name);
                dict[@"name"] = name;
            }
        }
        NSLog(@"%@",res);
        [res writeToFile:@"/Users/liuzhenlin/Desktop/123.txt" atomically:NO];
    }
    return 0;
}
